package com.haipin.zhenxuan;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.app.Activity;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AbsoluteLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {
   //18829553960     123456
	private WebView webview;
	String TAG = "MainActivity";
	private ProgressBar mProgressBar;
	private WebsiteChangeListener mWebsiteChangeListener;
	final String url = "http://juzhou.ctigood.cn/?f=a";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intView();
	}

	@Override
	protected void onDestroy() {

		if(webview != null)
		{
			webview.destroy();;
			webview = null;
		}
		super.onDestroy();
	}

	/**
	 *初始化视图
	 */
	private void intView() {

		initWebView();
		initProgressBar();
		webview.addView(mProgressBar);
	}

	private void initProgressBar() {


		//================顶部进度条的初始化===================
		mProgressBar = new ProgressBar(MainActivity.this, null,
				android.R.attr.progressBarStyleHorizontal);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, 8);
		mProgressBar.setLayoutParams(layoutParams);

		Drawable drawable = this.getResources().getDrawable(
				R.drawable.web_progress_bar_states);
		mProgressBar.setProgressDrawable(drawable);
	}

	private void initWebView() {

		webview = (WebView)findViewById(R.id.wv);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setAllowContentAccess(true);
		webview.getSettings().setAllowFileAccess(true);
		webview.getSettings().setAllowUniversalAccessFromFileURLs(false);
		webview.getSettings().setAllowFileAccessFromFileURLs(false);
		webview.getSettings().setBlockNetworkImage(false);
		webview.getSettings().setBlockNetworkLoads(false);
		//gxw- webview.getSettings().setFantasyFontFamily();
		webview.getSettings().setDatabaseEnabled(true);
		webview.getSettings().setDefaultTextEncodingName("UTF-8");
		webview.getSettings().setDisplayZoomControls(true);
		webview.getSettings().setDomStorageEnabled(true);
		//webview.getSettings().setFantasyFontFamily();
		webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webview.getSettings().setSupportMultipleWindows(true);
		webview.getSettings().setLightTouchEnabled(false);
		webview.getSettings().setLoadWithOverviewMode(false);
		webview.getSettings().setLoadsImagesAutomatically(true);
		webview.getSettings().setUseWideViewPort(true);
		webview.getSettings().setSupportZoom(false);
		webview.getSettings().setBuiltInZoomControls(true);
		webview.getSettings().setAppCacheEnabled(false);
		//webview.getSettings().setLightTouchEnabled(false);
		webview.requestFocus();
		webview.setWebViewClient(new MyWebViewClient());
		webview.setWebChromeClient(webChromeClient);
		webview.loadUrl(url);

		/*gxw-s

		Map<String, String> extraHeaders;
		extraHeaders = new HashMap<String, String>();
		extraHeaders.put("Authorization", "Android");
//webview.loadUrl("http://192.168.1.107:8080/#/pay?&deviceIMEI=1860247189D0ADB4&Authorization=android",extraHeaders);
		gxw-e*/


	}

	public void setWebsiteChangeListener(WebsiteChangeListener websiteChangeListener) {
		this.mWebsiteChangeListener = websiteChangeListener;

	}
	public interface WebsiteChangeListener {
		void onWebsiteChange(String title);

		void onUrlChange(String url);
//        void onWebsiteChangeBackTop();
	}

	WebChromeClient webChromeClient = new WebChromeClient() {

		//=========HTML5定位==========================================================
		//需要先加入权限
		//<uses-permission android:name="android.permission.INTERNET"/>
		//<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
		//<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
		@Override
		public void onReceivedIcon(WebView view, Bitmap icon) {
			super.onReceivedIcon(view, icon);
		}

		@Override
		public void onGeolocationPermissionsHidePrompt() {
			super.onGeolocationPermissionsHidePrompt();
		}

		@Override
		public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) {
			callback.invoke(origin, true, false);//注意个函数，第二个参数就是是否同意定位权限，第三个是是否希望内核记住
			super.onGeolocationPermissionsShowPrompt(origin, callback);
		}
		//=========HTML5定位==========================================================


		//=========多窗口的问题==========================================================
		@Override
		public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
			WebView.HitTestResult result = view.getHitTestResult();
			String data = result.getExtra();
			view.loadUrl(data);
			return true;
		}
		//=========多窗口的问题==========================================================

		//=========顶部进度条的进度更新===============================
		@Override
		public void onProgressChanged(WebView view, int newProgress) {

			if (newProgress == 100) {
				mProgressBar.setVisibility(View.GONE);
//                isNotTop = true;
			} else {
				if (mProgressBar.getVisibility() == View.GONE)
					mProgressBar.setVisibility(View.VISIBLE);
//                if (isNotTop && mProgressBar.getVisibility() == VISIBLE) {
//                    if (mWebsiteChangeListener != null) {
//                        mWebsiteChangeListener.onWebsiteChangeBackTop();
//                        isNotTop = false;
//                    }
//                }
				mProgressBar.setProgress(newProgress);
			}
			super.onProgressChanged(view, newProgress);
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle(view, title);
		if (mWebsiteChangeListener != null) {
				mWebsiteChangeListener.onWebsiteChange(title);
			}
		}
	};
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//return super.onKeyDown(keyCode, event);
		if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
			webview.goBack();
			Log.i(TAG,"goBack");
			return true;
		}
		else if((keyCode == KeyEvent.KEYCODE_BACK) && !(webview.canGoBack()))
		{
			MainActivity.this.finish();
		}
		return false;
	}



	public class MyWebViewClient extends WebViewClient{

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
		/*gxw-s 可能要恢复	Log.i(TAG,url);
			return super.shouldOverrideUrlLoading(view, url);*/


			view.loadUrl(url);
			if (mWebsiteChangeListener != null) {
				mWebsiteChangeListener.onUrlChange(url);
			}
			return true;
		}
		
	}
/*
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) mProgressBar.getLayoutParams();
		lp.x = l;
		lp.y = t;
		mProgressBar.setLayoutParams(lp);
		super.onScrollChanged(l, t, oldl, oldt);

	}*/

}
